package grdnlog.service;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.springframework.social.support.URIBuilder;
import org.springframework.social.twitter.api.Tweet;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

public class TwitterHelper {
	
	public List<Tweet> getUserTimeline(TwitterTemplate template, String screenName, int count) {
		MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
		parameters.set("screen_name", screenName);
		parameters.set("count", String.valueOf(count));
		
		return template.getRestTemplate().getForObject(buildUri("statuses/user_timeline.json", parameters), TweetList.class);
	}
	
	public List<Tweet> getUserTimeline(TwitterTemplate template, String screenName, int count, String sinceId) {
		MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
		parameters.set("screen_name", screenName);
		parameters.set("count", String.valueOf(count));
		parameters.set("since_id", sinceId);
		
		return template.getRestTemplate().getForObject(buildUri("statuses/user_timeline.json", parameters), TweetList.class);
	}
	
	protected URI buildUri(String path, MultiValueMap<String, String> parameters) {
		return URIBuilder.fromUri(API_URL_BASE + path).queryParams(parameters).build();
	}
	
	private static final String API_URL_BASE = "https://api.twitter.com/1/";
	
	@SuppressWarnings("serial")
	private static class TweetList extends ArrayList<Tweet> {}
}
